package io.github.hayeka.excel.model;

import io.github.hayeka.excel.context.ExcelApplicationContext;
import io.github.hayeka.excel.domain.SheetInformation;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * program:
 * description:
 * author:
 * create: 2021/8/27
 **/
public class Excel {

    private static Map<Class, SheetInformation> map = ExcelApplicationContext.getMap();
    private XSSFWorkbook excel;
    private List<Sheet> list = null;

    private int titleBeginIndex = 0;
    private int dataBeginIndex = 1;

    private Excel() {
    }

    public Excel(Sheet sheet) {
        this.list = new ArrayList<Sheet>();
        this.list.add(sheet);
    }

    public Excel(List<Sheet> list) {
        this.list = list;
    }


    public File getFileAtFilePath(String filePath, String fileName) {
        if (!filePath.endsWith("/")) {
            filePath = filePath + "/";
        }
        //初始化文件
        excel = new XSSFWorkbook();
        try {
            return makeExcel(excel, filePath, fileName);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private File makeExcel(XSSFWorkbook excel, String filePath, String fileName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
        for (Sheet fakerSheet : list) {
            Class clazz = fakerSheet.getClazz();
            XSSFSheet sheet = excel.createSheet(map.get(clazz).getSheetName());
            //标题
            XSSFRow row = sheet.createRow(titleBeginIndex);
            List<String> titleNameList = map.get(clazz).getTitleNameList();
            for (int i = 0; i < titleNameList.size(); i++) {
                //有几列就设置几行
                sheet.setColumnWidth(i, 20 * 256 * 1);
                //
                row.createCell(i).setCellValue(titleNameList.get(i));
            }
            //内容
            List<String> fieldNameList = map.get(clazz).getFieldNameList();
            for (Object o : fakerSheet.getList()) {
                XSSFRow rowData = sheet.createRow(dataBeginIndex++);
                for (int i = 0; i < fieldNameList.size(); i++) {
                    Method method = clazz.getMethod(methodNameStr(fieldNameList.get(i)));
                    Object invoke = method.invoke(o);
                    rowData.createCell(i).setCellValue((String) invoke);
                }
            }
            //重置下第多个sheet的data初始行
            dataBeginIndex = 1;
        }
        return finalFile(filePath, fileName);
    }

    private File finalFile(String filePath, String fileName) throws IOException {
        File file = new File(filePath + fileName);
        FileOutputStream stream = new FileOutputStream(file);
        excel.write(stream);
        stream.close();
        return file;
    }


    private String methodNameStr(String field) {
        return "get" + String.valueOf(field.charAt(0)).toUpperCase() + field.substring(1);
    }
}





















